capture	log close
log using MAR08-CondtionModel,  replace text

//  program:    Stata 
//  task:       Cross-Sectional Models 
//  project:    Markets  

version
clear all
macro drop _all
set linesize 80
set more off
local tag " 06-29-24| Cleaned 06-29-24"
local file "MAR08-CondtionModel"
local note "|`tag' | `file'"
local opt "noparen sideway excel noaster  bdec(2)  sdec(2)  pdec(3)   adec(2) e(r2) stats(coef se pval)"
local dv "lrhom"
local iv "fraser"
local iv2 "infantmort" 
local cont "edu unemp  popdense perurban  sexratio"
local icid " i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 i18 i19 i20 i21 i22 i23 i24 i25 i26 i27 i28 i29 i30 i31 i32 i33 i34 i35 i36 i37 i38 i39 i40 i41 i42 i43 i44 i45 i46 i47 i48 i49 i50 i51 i52 i53 i54 i55 i56 i57 i58 i59 i60 i61 i62 i63 i64 i65 i66 i67 i68 i69 i70 i71 i72 i73 i74 i75 i76 i77 i78 i79 i80 i81 i82 i83 i84 i85  i87 i88"
local iyear " x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 "

//	#0
//	loading panel data 
use MAR06-PanelModels.dta, clear 

//	#1
//	LAC nations
gen LAC=. 
replace LAC=1 if subregion=="Latin America and the Caribbean"
replace LAC=1 if region_un_sub==102
replace LAC=1 if region_un_sub==103
replace LAC=1 if region_un_sub==104

tab nation LAC, m 
replace LAC=0 if LAC==. 

//	#2
//	Conditioned sample w/ only and w/o LAC Fraser 
xtreg `dv' `iv' `iv2' `cont' if LAC==1, fe cluster(CID)
outreg2 using `file', `opt' replace addnote(LAC_conditioned) 
xttest3
qui reg `dv' `iv' `iv2' `cont' if LAC==1 
vif
xtserial `dv' `iv' `iv2' `cont' if LAC==1

xtreg `dv' `iv' `iv2' `cont' if LAC==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(NO_LAC_conditioned) 
xttest3
qui reg `dv' `iv' `iv2' `cont' if LAC==0
vif
xtserial `dv' `iv' `iv2' `cont' if LAC==0

//	#3
//	Conditioned sample w/ only and w/o LAC Heritage 
xtreg `dv' heritage `iv2' `cont' if LAC==1, fe cluster(CID)
outreg2 using `file', `opt' append addnote(LAC_conditioned) 
xttest3
qui reg `dv' heritage `iv2' `cont' if LAC==1 
vif
xtserial `dv' heritage `iv2' `cont' if LAC==1

xtreg `dv' heritage `iv2' `cont' if LAC==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(NO_LAC_conditioned) 
xttest3
qui reg `dv' heritage `iv2' `cont' if LAC==0
vif
xtserial `dv' heritage `iv2' `cont' if LAC==0

//	#4
//	Conditioned sample w/ only and w/o LAC ROL 
xtreg `dv' rol `iv2' `cont' if LAC==1, fe cluster(CID)
outreg2 using `file', `opt' append addnote(LAC_conditioned) 
xttest3
qui reg `dv' rol `iv2' `cont' if LAC==1 
vif
xtserial `dv' rol `iv2' `cont' if LAC==1

xtreg `dv' rol `iv2' `cont' if LAC==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(NO_LAC_conditioned) 
xttest3
qui reg `dv' rol `iv2' `cont' if LAC==0
vif
xtserial `dv' rol `iv2' `cont' if LAC==0

//	#5
//	Fraser set up 
gen Lfraser=. 
sum fraser, de
replace Lfraser=1 if fraser<=r(p50)
replace Lfraser=0 if fraser>r(p50)


/*//	#6
//	Conditioned INteractions 
xtreg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==1 & LAC==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser_Inter) 

xtreg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==0 & LAC==1, fe cluster(CID)
outreg2 using `file', `opt' append addnote(HFraser_Inter) 


xtreg `dv' c.`iv'##c.lgdp `iv2' `cont' if Lfraser==1 & LAC==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser_Inter) 

xtreg `dv' c.`iv'##c.lgdp `iv2' `cont' if Lfraser==0 & LAC==1, fe cluster(CID)
outreg2 using `file', `opt' append addnote(HFraser_Inter) 
*/

//	#7
//	Fraser High
xtreg `dv' `iv' `iv2' `cont' if Lfraser==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser) 
xttest3
qui reg `dv' `iv' `iv2' `cont' if Lfraser==0
vif
xtserial `dv' `iv' `iv2' `cont' if Lfraser==0


//	#8
//	Fraser Low 
xtreg `dv' `iv' `iv2' `cont' if Lfraser==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser) 
xttest3
qui reg `dv' `iv' `iv2' `cont' if Lfraser==1
vif
xtserial `dv' `iv' `iv2' `cont' if Lfraser==1

//	#9
//	Mod models with fraser  high`
xtreg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(LFraser_Inter) 
xttest3
qui reg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==0
vif

//	#10
//	Mod models with fraser low 
xtreg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser_Inter) 
xttest3
qui reg `dv' c.`iv'##c.`iv2' `cont' if Lfraser==1
vif

//	#11
//	GDP low and low 
xtreg `dv' c.`iv'##c.lgdp `iv2' `cont' if Lfraser==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(LFraser_Inter_GDP) 
xttest3
//	 high 
xtreg `dv' c.`iv'##c.lgdp `iv2' `cont' if Lfraser==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(HFraser_Inter_GDP) 
xttest3


//	#12
//	all 
xtreg `dv' c.`iv'##c.lgdp c.`iv'##c.`iv2' `cont' if Lfraser==0, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(IN_LFraser_Inter_GDP) 
xtreg `dv' c.`iv'##c.lgdp c.`iv'##c.`iv2' `cont' if Lfraser==1, fe cluster(CID) 
outreg2 using `file', `opt' append addnote(IN_HFraser_Inter_GDP) 

//	#13
//	mediating models high fraser 
preserve
drop if Lfraser!=0 
sem ( `iv'-> `iv2' , ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(LF_`iv'->`iv2')
restore 
/* ML not possible to obtain 
preserve 
drop if Lfraser!=1
sem ( `iv'-> `iv2' , ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(LF_`iv'->`iv2')
restore 
*/

//	#14
//	fraser^2 
tempfile hold
xtreg `dv' c.`iv'##c.`iv' `iv2' `cont', fe cluster(CID) 
outreg2 using `file', append `opt'  addnote(`iv'^2)
margins, at(fraser = (0(1)10)) saving(`hold', replace) 
preserve 
use `hold', clear 
gen model=1 
gen varX="Margin"
save ppFraser2.dta, replace 
restore 
marginsplot, name(p1, replace)
margins, dydx(fraser) at(fraser = (0(1)10))
preserve 
use `hold', clear 
gen model=1 
gen varX="DYDX"
save dydxFraser2.dta, replace 
restore 
marginsplot, name(p2, replace) 



//	#15
//	save and close 
save `file'.dta, replace
log close 
//